Ext.define('crm.view.CustomerList', {
    extend: 'Ext.Panel',
    xtype: 'customerlist',
    requires: [
        'crm.model.Customer', 'crm.view.Customer'
    ],
    config: {
        layout: 'card',
        groupByDay: true,
        loadingText: '加载中...'
    },
    initialize: function () {
        this.callParent();
        this.searchstore = new Ext.data.Store({
            model: 'crm.model.Customer',
            autoLoad: false,
            sorters: '',
            proxy: {
                type: 'ajax',
                url:server+ '/ext/com.cinsea.mobile.ext.CrmAction',
                reader: {
                    type: 'json',
                    totalProperty: 'totalCount',
                    rootProperty: 'result'
                }

            }
        })
        this.list = new Ext.List({
            itemTpl: '<span class="name">{title}</span> ',
            loadingText: false,
            plugins: [
                {
                    xclass: 'Ext.plugin.ListPaging',
                    autoPaging: false,
                    loadMoreText: '更多',
                    noMoreRecordsText: ''
                },
                {
                    xclass: 'Ext.plugin.PullRefresh',
                    pullRefreshText: '下拉刷新...',
                    releaseRefreshText: '释放停止...',
                    loadingText: '加载中...',
                    snappingAnimationDuration: 10
                }
            ],
            store: this.searchstore
        });


        this.list.on('itemtap', this.onSelect, this);
        this.listpanel = new Ext.Panel({
            items: this.list,
            layout: 'fit'
        });


        this.listpanel.add({
            xtype: 'toolbar',
            docked: 'top',
            layout: { pack: 'left' },
            items: [
                {
                    ui: 'back',
                    text: '返回',
                    scope: this,
                    handler: function () {
                        this.getParent().setActiveItem(this.prevCard, {
                            type: 'slide',
                            reverse: true,
                            scope: this,
                            after: function () {
                                this.destroy();
                            }
                        });
                    }
                },
                { xtype: 'spacer' },
                {
                    xtype: 'searchfield',
                    width: 200,
                    name: 'q',
                    placeHolder: '客户名称',
                    listeners: {
                        change: this.onFieldChange,
                        keyup: function (field, e) {
                            var key = e.browserEvent.keyCode;

                            // blur field when user presses enter/search which will trigger a change if necessary.
                            if (key === 13) {
                                field.blur();
                            }
                        },
                        scope: this
                    }
                }
            ]
        });

        // Take off the spinner
        //this.list.el.unmask();

        this.add(this.listpanel);

    },
    onFieldChange: function (comp, value) {
        this.searchstore.getProxy().setExtraParam('q', value);
        this.searchstore.load();
    },
    onSelect: function (v, i) {
        record = this.searchstore.getAt(i);
        if (record !== undefined) {
            if (record.data.datatype == '2') url = server+"/mobile/wfprocess.do";
            else if (record.data.datatype == '3') url = server+"/mobile/document.do";
            else  url = server+"/mobile/process.do";
            Ext.Ajax.request({
                scope: this,
                url: url + "?action=getformdata&processid=" + record.data.processid,
                success: function (res) {
                    obj = Ext.decode(res.responseText);
                    if (record.data.datatype == '2') {
                        var sessionCard = new Wfprocess({
                            remoteObj: obj,
                            processid: record.data.processid,
                            prevCard: this.listpanel,
                            record: record
                        });
                        sessionCard.renderform(obj);
                        sessionCard.setPrevcard(this.listpanel);
                        sessionCard.setProcessid(record.data.processid);
                    } else if (record.data.datatype == '3') {
                        var sessionCard = new Document({
                            remoteObj: obj,
                            processid: record.data.processid,
                            prevCard: this.listpanel,
                            record: record
                        });
                        sessionCard.setContent(obj);
                        sessionCard.setPrevcard(this.listpanel)
                    } else {
                        var sessionCard = new crm.view.Customer({
                            remoteObj: obj,
                            processid: record.data.processid,
                            prevCard: this.listpanel,
                            record: record
                        });
                        sessionCard.renderform(obj);
                        sessionCard.setPrevcard(this.listpanel);
                        sessionCard.setProcessid(record.data.processid);
                    }
                    this.setActiveItem(sessionCard, 'slide');

                }
            });

        }

    },
    setPrevcard: function (prevc,action) {
        this.prevCard = prevc;
        if(action=='getcrmfavor')
        this.listpanel.down('searchfield').setHidden(true);

        this.searchstore.getProxy().setExtraParam('action', action);
                this.searchstore.load();
    }

});
